<template>
	<view v-if="showThis" style="background-color: #F5F5F5; padding-bottom: 15px;">
		<view class="flex align-center justify-between padding">
			<text style="font-weight: bold;">正在抢购</text>
			<view style="color: #A5673F;">
				<text v-if="time.hour>0">{{time.hour}}小时</text>
				<text v-if="time.minute>0">{{time.minute}}分钟</text>
				<text v-if="time.second>0">{{time.second}}秒</text>
			</view>
		</view>
		<scroll-view scroll-x="true" style="white-space: nowrap; width: 100%;padding: 0 15px;">
			<view @tap="clickItem(item)" v-for="(item,index) in skill.skus" :key="item.skuId" style="margin-right: 15px;background-color: #FFFFFF;width: 120px;display: inline-block;">
				<image style="width: 120px;height: 120px;" :src="item.thumbnail"></image>
				<view style="color: #1A1A1A;
				font-size: 28upx;
				margin: 8upx 0;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box; 
				-webkit-line-clamp: 2;  
				-webkit-box-orient: vertical;">{{item.name}}</view>
				<view><text style="font-size: 11px;background-color: #A5673F;color: #FFFFFF;padding: 1px 5px;margin-right: 5px;">￥{{item.priceSkill}}</text><text
					 style="font-size: 11px;text-decoration: line-through;color: grey;">￥{{item.priceSale}}</text></view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	var intv = null;
	var cursurplus = null;
	export default {
		data() {
			return {
				time: null,
				show:true
			}
		},
		props: {
			skill: {
				type: Object,
				default: function() {
					return {};
				}
			}
		},
		mounted() {
			this.startInterval();
		},
		beforeDestroy() {
			if (intv != null) {
				clearInterval(intv)
			}
		},
		computed:{
			showThis:function() {
				return this.show && this.skill!=null && this.skill.skus!=null && this.skill.skus.length>0;
			}
		},
		watch: {
			skill(newObj, oldObj) {
				if (newObj.endTime != oldObj.endTime) {
					cursurplus = null;
					this.show = true;
					clearInterval(intv);
					this.startInterval();
				}
			}
		},
		methods: {
			clickItem(item) {
				uni.navigateTo({
					url: '/pages/product/detail?id=' + item.skuId+'&skillId='+item.skillId
				})
			},
			startInterval() {
				var that = this;
				var endTime = that.$convertTime(this.skill.endTime) ;
				intv = setInterval(function() {
					if(endTime==null) {
						endTime = that.$convertTime(that.skill.endTime) ;
					}
					var curTime = cursurplus != null ? cursurplus : new Date().getTime();
					cursurplus = curTime + 1000;
					//计算剩余时间
					var surplus = endTime- cursurplus;
					var hour = surplus / 1000 / 60 / 60; //剩余小时
					// console.log('hour - ' + hour);
					var inthour = parseInt(hour);
					var minute = inthour > 0 ? (hour % inthour * 60) : (hour * 60); //分钟
					// console.log('minute - ' + minute);
					var intminute = parseInt(minute);
					var second = intminute > 0 ? (minute % intminute * 60) : (minute * 60); //秒
					// console.log('second - ' + second);
					var intsecond = parseInt(second);
					// console.log('inthour:' + inthour + '    intminute:' + intminute + '   intsecond:' + intsecond);

					that.time = {
						hour: inthour,
						minute: intminute,
						second: intsecond
					}
					if (isNaN(inthour) && isNaN(intminute) && isNaN(intsecond)) {
						that.show = false;
						clearInterval(intv)
					}
					if (inthour == 0 && intminute == 0 && intsecond == 0) {
						that.show = false;
						clearInterval(intv)
					}
				}, 1000);
			}
		},
	}
</script>

<style>
</style>
